%%
%% This is file `verse.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% verse.dtx  (with options: `usc')
%% 
%% -----------------------------------------------------------------
%%   Author: Peter Wilson
%%   Maintainer: Will Robertson (will dot robertson at latex-project dot org)
%%           Herries Press
%%   Copyright 2001--2007 Peter R. Wilson
%% 
%%   This work may be distributed and/or modified under the
%%   conditions of the LaTeX Project Public License, either
%%   version 1.3c of this license or (at your option) any
%%   later version: <http://www.latex-project.org/lppl.txt>
%% 
%%   This work has the LPPL maintenance status "maintained".
%%   The Current Maintainer of this work is Will Robertson.
%% 
%%   This work consists of the files listed in the README file.
%% -----------------------------------------------------------------
%% 
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{verse}[2009/09/04 v2.4a verse typesetting]

\newcounter{vslineno}
\newcounter{poemline}
\newcounter{fvsline}
  \setcounter{fvsline}{0}
\newcounter{modulo@vs}
\newcounter{verse@envctr}\setcounter{verse@envctr}{0}
\newcommand*{\theHpoemline}{\arabic{verse@envctr}.\arabic{poemline}}

\newcommand{\poemlines}[1]{%
  \ifnum#1>\z@
    \setcounter{modulo@vs}{#1}
  \else
    \setcounter{modulo@vs}{0}
  \fi
}
\poemlines{0}

\newcommand*{\verselinenumfont}[1]{\def\vlnumfont{#1}}
\verselinenumfont{\rmfamily}

\newcommand*{\setverselinenums}[2]{%
  \setcounter{poemline}{#1}\addtocounter{poemline}{\m@ne}%
  \refstepcounter{poemline}%
  \ifnum\c@modulo@vs>\z@
    \@tempcnta #2\relax
    \divide\@tempcnta\c@modulo@vs
    \multiply\@tempcnta\c@modulo@vs
    \c@fvsline #2\relax
    \advance\c@fvsline-\@tempcnta
  \fi}

\newcommand{\getmodulo@vs}{\bgroup
  \ifnum\c@modulo@vs<\@ne%   no line numbers
  \else
    \ifnum\c@modulo@vs<\tw@% every line numbered
      \vlnumfont\thepoemline
    \else
      \@tempcnta\c@poemline
      \advance\@tempcnta -\c@fvsline
      \divide\@tempcnta\c@modulo@vs
      \multiply\@tempcnta\c@modulo@vs
      \advance\@tempcnta\c@fvsline
      \ifnum\@tempcnta=\c@poemline\vlnumfont\thepoemline\fi
    \fi
  \fi
\egroup}

\newif\ifaltindent
  \altindentfalse
\newif\ifpattern
  \patternfalse
\newif\ifstarpattern
  \starpatternfalse

\newlength{\versewidth}

\newlength{\vgap} \setlength{\vgap}{1.5em}
\newcommand{\vin}{\hspace*{\vgap}}
\newlength{\vindent} \setlength{\vindent}{2\vgap}
\newlength{\stanzaskip} \setlength{\stanzaskip}{0.75\baselineskip}

\newlength{\vleftskip}
  \setlength{\vleftskip}{30pt}
\newlength{\vrightskip}
  \setlength{\vrightskip}{10pt}

\newcommand{\flagverse}[1]{\hskip-\vleftskip\llap{#1}\hskip\vleftskip\ignorespaces}

\newcommand*{\verselinebreak}[1][\z@]{\newline\hspace*{#1}\ignorespaces}
\newcommand{\incr@vsline}{%
  \refstepcounter{poemline}%
  \stepcounter{vslineno}}

\newcommand{\@vsifbang}[1]{\@ifnextchar !{\@firstoftwo{#1}}}
\newcommand{\@vsifgt}[1]{\@ifnextchar >{\@firstoftwo{#1}}}

\newcommand*{\@vstypelinenumright}{%
  \hfill\rlap{\kern\vrightskip\kern\rightmargin\getmodulo@vs}}
\newcommand*{\@vstypelinenumleft}{%
  \hfill\rlap{\kern-\textwidth\kern-\vrightskip\getmodulo@vs}}
\newcommand*{\verselinenumbersright}{\def\@vstypelinenum{\@vstypelinenumright}}
\newcommand*{\verselinenumbersleft}{\def\@vstypelinenum{\@vstypelinenumleft}}
\verselinenumbersright

\newcommand{\@vscentercr}{%
  \ifhmode \unskip\else \@nolnerr\fi
  \@vstypelinenum%
%%%%  \hfill\rlap{\kern\vrightskip\kern\rightmargin\getmodulo@vs}%
  \@vsifgt{\verselinebreak}{%
  \incr@vsline
  \par\@ifstar{\nobreak\@vsxcentercr}{%
    \@vsifbang{\@ifnextchar[ {\@vsicentercr}{}}{\@vsxcentercr}}}}
\newcommand{\@vsxcentercr}{\addvspace{-\parskip}%
  \@ifnextchar[ {\@vsicentercr}{\start@vsline}}
\def\@vsicentercr[#1]{\vskip #1\ignorespaces \start@vsline}
\newcommand{\start@vsline}{%
  \ifaltindent\ifodd\c@vslineno\else\vin\fi\fi%
  \ifpattern\get@vsindent\fi%
  \ifstarpattern\getstar@vsindent\fi}

\renewenvironment{verse}[1][\linewidth]{
  \stepcounter{verse@envctr}%
  \setcounter{poemline}{0}\refstepcounter{poemline}%
  \setcounter{vslineno}{1}%
  \let\\=\@vscentercr
  \list{}{\itemsep \z@
          \itemindent  -\vindent%
          \listparindent\itemindent
          \parsep       \stanzaskip
          \ifdim #1 < \linewidth
            \rightmargin        \z@
            \setlength{\leftmargin}{\linewidth}%
            \addtolength{\leftmargin}{-#1}%
            \addtolength{\leftmargin}{-0.5\leftmargin}%
          \else
            \rightmargin        \leftmargin
          \fi
          \addtolength{\leftmargin}{\vindent}}
  \item[]}{\endlist}

\newenvironment{altverse}%
  {\starpatternfalse\patternfalse\altindenttrue\setcounter{vslineno}{1}}%
  {\altindentfalse}

\newcommand{\@nameedef}[1]{
  \expandafter\protected@edef\csname #1\endcsname}

\newif\ifbounderror
  \bounderrorfalse
\newif\ifinteger

\newcounter{chrsinstr}  % CHARactersINSTRing

\newcommand{\newarray}[3]{%
  \@nameedef{#1-low}{#2}%
  \@nameedef{#1-high}{#3}%
  \ifnum #3<#2
    \PackageError{verse}{Limits for array #1 are in reverse order}{\@ehc}%
  \fi
}

\newcommand{\stringtoarray}[2]{%
  \def\@vsarrayname{#1}%
  \protected@edef\the@vsstring{#2}%
  \newarray{\@vsarrayname}{1}{1}%
  \@ifmtarg{#2}{%
    \c@chrsinstr \z@
    \@namedef{\@vsarrayname-1}{}
  }{%
    \c@chrsinstr \@ne
    \expandafter\@vsstringtoarray \the@vsstring\@vsend
  }
}

\def\@vsstringtoarray #1#2\@vsend{%
  \@namedef{\@vsarrayname-\the\c@chrsinstr}{#1}
  \@nameedef{\@vsarrayname-high}{\the\c@chrsinstr}
  \@ifmtarg{#2}{%
    \def\@vsinext{}%
  }{%
    \advance\c@chrsinstr \@ne
    \def\@vsinext{%
      \@vsstringtoarray #2\@vsend%
    }%
  }
  \@vsinext
}

\newcommand{\setarrayelement}[3]{%
  \checkarrayindex{#1}{#2}%
  \@nameedef{#1-#2}{#3}%
}

\newcommand{\getarrayelement}[3]{%
  \checkarrayindex{#1}{#2}%
  \protected@edef#3{\@nameuse{#1-#2}}%
}

\newcommand{\checkarrayindex}[2]{%
  \bounderrorfalse
  \expandafter\ifx\csname #1-low\endcsname\relax%
    \ifpattern\else
      \PackageError{verse}{No array called #1}{\@ehc}%
    \fi
    \bounderrortrue
  \fi
  \ifnum #2<\@nameuse{#1-low}\relax%
    \ifpattern\else
      \PackageError{verse}{Index #2 outside limits for array #1}{\@ehc}%
    \fi
    \bounderrortrue
  \fi
  \ifnum #2>\@nameuse{#1-high}\relax%
    \ifpattern\else
      \PackageError{verse}{Index #2 outside limits for array #1}{\@ehc}%
    \fi
    \bounderrortrue
  \fi
}

\begingroup
\catcode`\Q=3
\long\gdef\@ifmtarg#1{\@xifmtarg#1QQ\@secondoftwo\@firstoftwo\@nil}
\long\gdef\@xifmtarg#1#2Q#3#4#5\@nil{#4}
\long\gdef\@ifnotmtarg#1{\@xifmtarg#1QQ\@firstofone\@gobble\@nil}
\endgroup

\newcommand{\arraytostring}[2]{%
  \def#2{}%
  \c@chrsinstr = \@nameuse{#1-low}%
  \@vsarraytostring{#1}{#2}%
}

\newcommand{\@vsarraytostring}[2]{%
  \ifnum\c@chrsinstr>\@nameuse{#1-high}\else
    \protected@edef#2{#2\@nameuse{#1-\thechrsinstr}}%
    \advance\c@chrsinstr\@ne%
    \@vsarraytostring{#1}{#2}%
  \fi%
}

\newcommand{\checkifinteger}[1]{%
  \protected@edef\@vsa{#1}%
  \ifcat _\ifnum9<1\gobm{#1} _\else A\fi
    \integertrue%
  \else
    \integerfalse%
  \fi%
}
\newcommand{\gobm}[1]{#1}

\newcommand{\indentpattern}[1]{%
  \stringtoarray{Array@vs}{#1}}

\newcommand{\get@vsindent}{%
  \getarrayelement{Array@vs}{\number\value{vslineno}}{\@vspat}%
  \ifbounderror
    \arraytostring{Array@vs}{\@vsp@t}%
    \PackageWarning{verse}{%
      Index `\thevslineno' for pattern `\@vsp@t' is out of bounds}%
    \def\@vspat{0}%
  \else
    \checkifinteger{\@vspat}%
    \ifinteger\else
      \arraytostring{Array@vs}{\@vsp@t}%
      \PackageWarning{verse}{%
       `\@vspat' at index `\thevslineno' in pattern `\@vsp@t' is not a digit}%
      \def\@vspat{0}%
    \fi
  \fi
  \ifcase\@vspat\else\hspace*{\@vspat\vgap}\fi}
\newcommand{\getstar@vsindent}{%
  \expandafter\ifx\csname Array@vs-high\endcsname\relax
    \PackageError{verse}{A pattern has not been specified}{\@ehc}
  \else
    \ifnum\c@vslineno>\@nameuse{Array@vs-high}%
      \setcounter{vslineno}{1}%
     \fi
     \get@vsindent
  \fi}

\newenvironment{patverse}%
  {\starpatternfalse\patterntrue\altindentfalse\setcounter{vslineno}{1}}%
  {\patternfalse}

\newenvironment{patverse*}%
  {\starpatterntrue\patternfalse\altindentfalse\setcounter{vslineno}{1}}%
  {\starpatternfalse}

\newcommand{\poemtitle}{\par%
  \secdef\@vsptitle\@vssptitle}
\newcommand{\poemtoc}{section}

\def\@vsptitle[#1]#2{%
  \@nameuse{phantomsection}%
  \addcontentsline{toc}{\poemtoc}{#1}
  \poemtitlemark{#1}%
  \@vstypeptitle{#2}
  \@afterheading}

\def\@vssptitle#1{%
  \@vstypeptitle{#1}
  \@afterheading}

\newcommand{\@vstypeptitle}[1]{%
  \vspace{\beforepoemtitleskip}
  {\poemtitlefont #1\par}
  \vspace{\afterpoemtitleskip}
}

\newcommand{\poemtitlefont}{\normalfont\large\bfseries\centering}
\newcommand{\poemtitlemark}[1]{}

\newlength{\beforepoemtitleskip}
  \setlength{\beforepoemtitleskip}{3.5ex \@plus 1ex \@minus .2ex}
\newlength{\afterpoemtitleskip}
  \setlength{\afterpoemtitleskip}{2.3ex \@plus.2ex}

\endinput
%%
%% End of file `verse.sty'.
